package com.ledim.widget.loadmore;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.activity.FeedbackActivity;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.LogUtil;
import com.letv.android.young.client.R;

/**
 * Created by ccx on 16/6/21.
 * 乐次元上拉加载view
 */
public class LedimLoadMoreFooterView extends FrameLayout
        implements LoadMoreUIHandler, LoadMoreStateCode, View.OnClickListener {

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_LOADING = 2;
    public final static int STATE_OVER = 3;

    public final static int STATE_OVER1 = 4;
    public final static int STATE_OVER2 = 5;

    private Context mContext;

    private View mContentView;
    private View mLoad;
    private ImageView mProgressBar;
    private TextView mHintView;
    private ImageView mImage;
    private AnimationDrawable mAnimationDrawable = null;
    private AnimationDrawable mLoadAnimationDrawable = null;
    private View ll_end;
    private FrameLayout moreView;
    private ImageView mGoFeedbackImage;

    public void setNeedFeedback(boolean needFeedback) {
        this.mNeedFeedback = needFeedback;
    }

    private boolean mNeedFeedback;

    public LedimLoadMoreFooterView(Context context) {
        super(context);
        initView(context);
    }

    public LedimLoadMoreFooterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public LedimLoadMoreFooterView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    public void setState(int state) {
        mHintView.setVisibility(View.VISIBLE);
        //		mProgressBar.setVisibility(View.INVISIBLE);
        mHintView.setTextColor(Color.parseColor("#333333"));
        if (mLoadAnimationDrawable.isRunning()) {
            mLoadAnimationDrawable.stop();
        }
        if (state == STATE_READY) {
            mLoad.setVisibility(View.VISIBLE);
            mImage.setVisibility(View.GONE);
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(R.string.xlistview_footer_hint_ready);
            mProgressBar.setVisibility(View.GONE);
        } else if (state == STATE_LOADING) {
            //			mProgressBar.setVisibility(View.VISIBLE);
            mHintView.setText("大波内容正在赶来…");
            mProgressBar.setVisibility(View.VISIBLE);
            mLoadAnimationDrawable.start();
        } else if (state == STATE_OVER) {
            mLoad.setVisibility(View.GONE);
            mImage.setVisibility(View.VISIBLE);
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(R.string.xlistview_footer_hint_over);
            mHintView.setTextColor(Color.parseColor("#888888"));
            mProgressBar.setVisibility(View.GONE);
        } else if (state == STATE_OVER2) {
            mImage.setImageResource(R.drawable.over_more2);
        } else {
            mLoad.setVisibility(View.VISIBLE);
            mImage.setVisibility(View.GONE);
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(R.string.xlistview_footer_hint_normal);
            mProgressBar.setVisibility(View.GONE);
        }
    }

    public void setBottomMargin(int height) {
        if (height < 0) {
            return;
        }
        if (height > DensityUtil.dip2px(getContext(), 160)) {
            return;
        }
        if (height < 10) {
            mImage.setImageResource(R.drawable.over_more1);
        }
        FrameLayout.LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.bottomMargin = height;
        mContentView.setLayoutParams(lp);
    }

    public int getBottomMargin() {
        FrameLayout.LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        return lp.bottomMargin;
    }

    /**
     * hide footer when disable pull load more
     */
    public void hide() {
        FrameLayout.LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.height = 0;
        mContentView.setLayoutParams(lp);
    }

    /**
     * show footer
     */
    public void show() {
        FrameLayout.LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.height = LayoutParams.WRAP_CONTENT;
        mContentView.setLayoutParams(lp);
    }

    private void initView(Context context) {
        mContext = context;
        moreView = (FrameLayout) LayoutInflater.from(mContext)
                                               .inflate(R.layout.home_xlistview_footer, null);
        addView(moreView);
        moreView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        mContentView = moreView.findViewById(R.id.xlistview_footer_content);
        mLoad = moreView.findViewById(R.id.xlistview_footer_load);
        mProgressBar = (ImageView) moreView.findViewById(R.id.xlistview_footer_progressbar);
        mHintView = (TextView) moreView.findViewById(R.id.xlistview_footer_hint_textview);
        mGoFeedbackImage = (ImageView) moreView.findViewById(R.id.iv_go_feedback);
        mGoFeedbackImage.setOnClickListener(this);

        ll_end = moreView.findViewById(R.id.ll_end);
        ll_end.setVisibility(GONE);
        mProgressBar.setImageResource(R.anim.refresh_animation);
        mLoadAnimationDrawable = (AnimationDrawable) mProgressBar.getDrawable();
        mLoadAnimationDrawable.setOneShot(false);
        hide();
    }

    public int getVisiableHeight() {
        return moreView.getHeight();
    }

    @Override
    public void onLoading(LoadMoreContainer container) {
        show();
        mProgressBar.setVisibility(VISIBLE);
        LogUtil.e("loadmore onLoading");
    }

    @Override
    public void onLoadFinish(LoadMoreContainer container, int errorCode, boolean hasMore) {
        LogUtil.e("loadmore onLoadFinish errorCode=" + errorCode);
        mHintView.setVisibility(VISIBLE);
        mGoFeedbackImage.setVisibility(GONE);
        mProgressBar.setVisibility(View.GONE);
        if (errorCode == STATE_REFRESH) { // 刷新列表把footer隐藏
            hide();
        } else if (errorCode == STATE_NORMAL) {
            show();
            if (!hasMore) {
                if (container instanceof LoadMoreContainerBase) {
                    if (((LoadMoreContainerBase) container).isOnePageData()) {
                        //仅有一页不展示footer
                        hide();
                    }
                }
                if (mNeedFeedback) {
                    mHintView.setVisibility(GONE);
                    mGoFeedbackImage.setVisibility(VISIBLE);
                } else {
                    mHintView.setText("还不满足嘛，小妖精~");
                }

            } else {
                mProgressBar.setVisibility(View.VISIBLE);
                mHintView.setText("大波内容正在赶来…");
            }
        } else if (errorCode == ERROR_UNKONW) {
            show();
            mHintView.setText(R.string.cube_views_load_more_error_unkonw);
        } else if (errorCode == ERROR_NET) {
            show();
            mHintView.setText(R.string.cube_views_load_more_error_net);
        } else if (errorCode == ERROR_SE) {
            show();
            mHintView.setText(R.string.cube_views_load_more_error_se);
        }
    }

    @Override
    public void onWaitToLoadMore(LoadMoreContainer container) {
        show();
        LogUtil.e("loadmore onWaitToLoadMore");
        mHintView.setText(R.string.cube_views_load_more_click_to_load_more);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(mContext, FeedbackActivity.class);
        mContext.startActivity(intent);
    }
}
